package com.pratices.cookie;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/local/change")
public class CookieChange extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断是否有name为locale的Cookie
        if (req.getCookies() != null) {
            for (Cookie cookie : req.getCookies()) {
                if (cookie.getName().equals("locale")) {
                    //如果存在，则判断值是否zh_CN是的话，则改为en_US。若是en_US的话，改为zh_CN
                    if (cookie.getValue().equals("zh_CN")) {
                        cookie.setValue("en_US");
                    }
                    else {
                        cookie.setValue("zh_CN");
                    }
                    resp.addCookie(cookie);
                }
            }
        }
    }

}
